package com.gqr.threadobjectclasscommonmethods;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 演示sleep不释放lock(lock 需要手动释放)
 */
public class SleepDontReleaseLock implements Runnable{

    private static final Lock lock = new ReentrantLock();


    public static void main(String[] args) {
        SleepDontReleaseLock releaseLock = new SleepDontReleaseLock();
        new Thread(releaseLock).start();
        new Thread(releaseLock).start();
    }
    @Override
    public void run() {
        lock.lock();
        System.out.println("线程"+Thread.currentThread().getName()+"获取了lock");
        try {
            Thread.sleep(5000);
            System.out.println("线程"+Thread.currentThread().getName()+"苏醒");

        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            // 变成规范 必须在finally释放掉锁
            lock.unlock();
        }
    }
}
